﻿using System;
using System.Collections.Specialized;
using System.Data;
using System.Xml;
using VitCode.BaseDataPageCreator;
using VitCode.BaseDataPageCreator.Setting;
using VitCode.Castle.DataAccess;
using VitCode.Castle.Helper;
using VitCode.Web.UI;

namespace AMS.Web.UserControls
{
    public partial class SubAssetListCtl : System.Web.UI.UserControl
    {
        private PageBuilder m_PageBuilder;
        private ObjXmlReader m_XmlReader;
        public string ClassName
        {
            get { return hdnClassName.Value; }
            set { hdnClassName.Value = value; }
        }

        public string TypeName
        {
            get { return hdnTypeName.Value; }
            set { hdnTypeName.Value = value; }
        }

        public string KeyProVal
        {
            get { return hdnKeyProVal.Value; }
            set { hdnKeyProVal.Value = value; }
        }

        public string KeyProName
        {
            get { return hdnKeyProName.Value; }
            set { hdnKeyProName.Value = value; }
        }

        public string HiddenColInfo
        {
            get { return hdnHiddenColInfo.Value; }
            set { hdnHiddenColInfo.Value = value; }
        }

        public string SettingXmlPath
        {
            get { return AppDomain.CurrentDomain.BaseDirectory + "AssetQuery\\ProSetting.xml"; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            pgList.DataSourceDelegate = new PaginationGrid.GridDataSourceDelegate(BindPgList);
        }

        public void BindData()
        {
            pgList.BindGrid();
        }


        protected override void OnInit(EventArgs e)
        {
            if (m_PageBuilder == null)
                m_PageBuilder = new PageBuilder(Page, SettingXmlPath);
            //PrintDataGrid();
            base.OnInit(e);
        }

        private StringCollection GetHiddenColNames()
        {
            string[] hiddenColNames = HiddenColInfo.Split(',');
            StringCollection sc = new StringCollection();
            if (HiddenColInfo.Length > 0)
            {
                foreach (string str in hiddenColNames)
                {
                    if (!sc.Contains(str))
                        sc.Add(str);
                }
            }
            return sc;
        }

        private DataTable GetListData(string strCondition)
        {
            DataSet ds;
            var acc = new BaseDataAccess();
            var sqlHelper = new SqlHelper();
            string xmlPath = AppDomain.CurrentDomain.BaseDirectory + "AssetQuery\\ProSetting.xml";
            m_XmlReader = new ObjXmlReader(xmlPath);
            XmlNode node = m_XmlReader.GetXmlNode("Setting/AssetSql");
            if (node != null)
            {
                string sql = m_XmlReader.GetNodeAttributeValue(node, "sql", true);
                sql = sql.Replace("\r\n", " ").Replace("\t", " ");
            try
            {
                ds = sqlHelper.ExecuteDataSet(acc.Session, sql + strCondition);
            }
            finally
            {
                acc.CloseSession();
            }
            return ds.Tables[0];
            }
            return null;
          
        }

        private object BindPgList()
        {   
            var acc = new BaseDataAccess();
            var m_SqlHelper = new SqlHelper();
            DataSet ds = m_SqlHelper.ExecuteDataSet(acc.Session, "select Barcode from AmsCore_AssetInfo where Id = '" + KeyProVal + "'");
            if(ds != null && ds.Tables[0].Rows.Count > 0)
            {
                string ProVal = ds.Tables[0].Rows[0][0].ToString();
                return GetListData(" where " + KeyProName + "='" + ProVal + "' ");
            }
            return GetListData("");
            
        }

        protected void pgList_OnGridShowData(object sender, PaginationGridShowDataArgs e)
        {
    
        }
    }
}